package com.eurotalkweather;

import java.io.BufferedInputStream;
import java.net.URI;
import java.text.DecimalFormat;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.xml.sax.InputSource;
import org.xmlpull.v1.sax2.Driver;

import com.eurotalkweather.R;

import android.os.AsyncTask;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WindowMain extends Activity {

    private boolean mainWindow = true; //To know where we are and update rightly the menu and animations.
    private boolean cityFound; //When a city typed matches with the adapter this boolean switches to true.
    private String myCurrentCity, apiKey = "b72988dc-53d9-486f-8887-295f82e81bf5";
    private String [][] citiesList;
    private String [] citiesNameList;
    private AutoCompleteTextView myAutoCompleteTextView;
    private Button myButtonGo;
    private ProgressBar myProgressBarLoading;
    private TextView myTextViewCityWeatherForecast, myTextViewTypeCity, myTextViewCopyright, myTextViewUps,
    		myTextViewD1Day, myTextViewD1Mon, myTextViewD1T, myTextViewD1WS,
    		myTextViewN1T, myTextViewN1WS,
    		myTextViewD2Day, myTextViewD2Mon, myTextViewD2T, myTextViewD2WS,
    		myTextViewN2T, myTextViewN2WS,
    		myTextViewD3Day, myTextViewD3Mon, myTextViewD3T, myTextViewD3WS,
    		myTextViewN3T, myTextViewN3WS,
    		myTextViewD4Day, myTextViewD4Mon, myTextViewD4T, myTextViewD4WS,
    		myTextViewN4T, myTextViewN4WS,
    		myTextViewD5Day, myTextViewD5Mon, myTextViewD5T, myTextViewD5WS,
    		myTextViewN5T, myTextViewN5WS;
    private ImageView myImageViewD1W, myImageViewN1W, myImageViewD2W, myImageViewN2W, myImageViewD3W, 
    		myImageViewN3W, myImageViewD4W, myImageViewN4W, myImageViewD5W, myImageViewN5W;
    private LinearLayout myLinearLayoutSearch, myLinearLayoutHead, myLinearLayoutMetOffice, myLinearLayoutWeatherTable,
    			myLinearLayoutUps;
    private RelativeLayout.LayoutParams myRelativeParamsTop = new RelativeLayout.LayoutParams(
	    RelativeLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT),
	    				myRelativeParamsCenter = new RelativeLayout.LayoutParams(
	    RelativeLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    private Animation myAnimationTranslateUp, myAnimationTranslateDown, myAnimationAlpha1to0, myAnimationAlpha0to1,
    myAnimationAlpha1to0BarProgress, myAnimationAlpha0to1BarProgress,
    myAnimationAlpha0to1WeatherTable, myAnimationAlpha1to0WeatherTable,
    myAnimationAlpha0to1Ups, myAnimationAlpha1to0NoResults;
    private Menu myMenu;
    private DecimalFormat myFormat = new DecimalFormat("#0");
    
    
    
    
    
    private class ParseXmlCities extends AsyncTask<Void, Void, Void> {
	protected Void doInBackground(Void... v) {
	    SAXHandlerCities mySAXHandlerCities = new SAXHandlerCities();
	    try {
		Driver myXMLReader = new Driver();
		myXMLReader.setContentHandler(mySAXHandlerCities);
		myXMLReader.parse(new InputSource(new BufferedInputStream(
			getResources().openRawResource(R.raw.sitelist))));
		//Since this sitelist is always the same it is stored offline for convenience.
		//It was got from http://partner.metoffice.gov.uk/public/val/wxfcs/all/xml/sitelist?key=b72988dc-53d9-486f-8887-295f82e81bf5

	    } catch (Throwable e) {
		System.out.println("Exception with XML processor: " + e);
	    }
	    citiesList = mySAXHandlerCities.getCitiesList();
	    citiesNameList = mySAXHandlerCities.getCitiesNameList();
	    return null;
	}
	protected void onPostExecute(Void v) {
	    myAutoCompleteTextView.setAdapter(new ArrayAdapter<String>(WindowMain.this,
		    R.layout.list_item, citiesNameList));
	    //I use setFocusableInTouchMode() instead of setFocusable()
	    //because of this: http://systemerrorcs.wordpress.com/2012/01/25/android-edittext-setfocusabletrue-does-not-work-if-you-remove-focus/
	    //The issue: http://code.google.com/p/android/issues/detail?id=2771
	    myAutoCompleteTextView.setEnabled(true);
	    myAutoCompleteTextView.setFocusableInTouchMode(true);
	    myAutoCompleteTextView.requestFocus();
	    myAutoCompleteTextView.setHint(R.string.type_a_place_hint);
	    myProgressBarLoading.startAnimation(myAnimationAlpha1to0BarProgress);
	    myProgressBarLoading.setVisibility(View.INVISIBLE);
	}
    }
    
    
    
    
    
    private class ParseXmlWeather extends AsyncTask<Integer, Void, String[][]> {
	protected void onPreExecute() {
	    myProgressBarLoading.startAnimation(myAnimationAlpha0to1BarProgress);
	    myProgressBarLoading.setVisibility(View.VISIBLE);
	}
	protected String[][] doInBackground(Integer... locationID) {
	    StringBuilder myURI = new StringBuilder("http://partner.metoffice.gov.uk/public/val/wxfcs/all/xml/")
	    	.append((int)locationID[0]).append("?res=daily&key=").append(apiKey);
	    DefaultHttpClient myDefaultHttpClient = new DefaultHttpClient();;
	    try {	    
		BasicHttpResponse myHttpResponse = (BasicHttpResponse) myDefaultHttpClient
			.execute(new HttpGet(new URI(myURI.toString())));
		HttpEntity myHttpEntity = myHttpResponse.getEntity();

		SAXHandlerWeather mySAXHandler = new SAXHandlerWeather();
		try {
		    Driver myXMLReader = new Driver();
		    myXMLReader.setContentHandler(mySAXHandler);
		    myXMLReader.parse(new InputSource(new BufferedInputStream(myHttpEntity.getContent())));
		} catch (Throwable e) {
		    System.out.println("Exception with XML processor: " + e);
		}
		return mySAXHandler.getWeather();
	    } catch (Exception e1) {
		e1.printStackTrace();
		return null;
	    }
	}
	protected void onPostExecute(String[][] result) {
	    updateWeatherTable(result);
	    myProgressBarLoading.startAnimation(myAnimationAlpha1to0BarProgress);
	    myProgressBarLoading.setVisibility(View.INVISIBLE);
	}
    }

    
    
    
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	//Screen configurations:
	DisplayMetrics myMetrics = getResources().getDisplayMetrics();
	Configuration myConfiguration = getResources().getConfiguration();
	//If the screen is 320x240 or 480x320 the landscape mode is not allowed:
	if(myConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE
		&& ((myMetrics.widthPixels == 240 && myMetrics.heightPixels == 320)
			|| (myMetrics.widthPixels == 320 && myMetrics.heightPixels == 240)
			|| (myMetrics.widthPixels == 320 && myMetrics.heightPixels == 480)
			|| (myMetrics.widthPixels == 480 && myMetrics.heightPixels == 320))) {
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    Toast.makeText(this, getString(R.string.screen_orientation_not_supported), Toast.LENGTH_LONG).show();
	}
	//If the screen is in landscape mode:
	else if(myConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
	    myRelativeParamsCenter.addRule(RelativeLayout.ABOVE, R.id.linearLayoutCenterReference);
	    myRelativeParamsCenter.setMargins(0, 0, 0, (int)(-100*myMetrics.density));
	}
	//If the screen is 320x240:
	if((myMetrics.widthPixels == 240 && myMetrics.heightPixels == 320)
		|| (myMetrics.widthPixels == 320 && myMetrics.heightPixels == 240)) {
	    myRelativeParamsCenter.addRule(RelativeLayout.ABOVE, R.id.linearLayoutCenterReference);
	    myRelativeParamsCenter.setMargins(0, 0, 0, (int)(-80*myMetrics.density));
	}
	//If the screen is other else mode:
	else myRelativeParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
	myRelativeParamsTop.addRule(RelativeLayout.ALIGN_PARENT_TOP);



	myAnimationTranslateUp = AnimationUtils.loadAnimation(this, R.anim.translate_up);
	myAnimationTranslateUp.setAnimationListener(new AnimationListener() {
	    @Override
	    public void onAnimationStart(Animation animation) {
	    }

	    @Override
	    public void onAnimationRepeat(Animation animation) {
	    }

	    @Override
	    public void onAnimationEnd(Animation animation) {
		mainWindow = false;
		myLinearLayoutSearch.clearAnimation();
		myLinearLayoutSearch.setLayoutParams(myRelativeParamsTop);
		requestNewWeatherTable();
	    }
	});;
	myAnimationTranslateDown = AnimationUtils.loadAnimation(this, R.anim.translate_down);
	myAnimationTranslateDown.setAnimationListener(new AnimationListener() {
	    @Override
	    public void onAnimationStart(Animation animation) {
	    }

	    @Override
	    public void onAnimationRepeat(Animation animation) {
	    }

	    @Override
	    public void onAnimationEnd(Animation animation) {
		mainWindow = true;
		myLinearLayoutSearch.clearAnimation();
		myLinearLayoutSearch.setLayoutParams(myRelativeParamsCenter);
		myLinearLayoutHead.startAnimation(myAnimationAlpha0to1);
		myTextViewTypeCity.startAnimation(myAnimationAlpha0to1);
		myLinearLayoutMetOffice.startAnimation(myAnimationAlpha0to1);
		myTextViewCopyright.startAnimation(myAnimationAlpha0to1);
		
		//To call setVisibility() is necessary because if not there are problems
		//with the animations and they are not executed well.
		myLinearLayoutHead.setVisibility(View.VISIBLE);
		myTextViewTypeCity.setVisibility(View.VISIBLE);
		myLinearLayoutMetOffice.setVisibility(View.VISIBLE);
		myTextViewCopyright.setVisibility(View.VISIBLE);
		myAutoCompleteTextView.setText(null);
	    }
	});;
	myAnimationAlpha0to1 = AnimationUtils.loadAnimation(this, R.anim.alpha0to1);
	myAnimationAlpha1to0 = AnimationUtils.loadAnimation(this, R.anim.alpha1to0);

	//These animations are the same than those of above but they are created specifically for the below views
	//because same animations used in several views is problematic and animations are executed badly.
	myAnimationAlpha0to1BarProgress = AnimationUtils.loadAnimation(this, R.anim.alpha0to1);
	myAnimationAlpha1to0BarProgress = AnimationUtils.loadAnimation(this, R.anim.alpha1to0);
	myAnimationAlpha0to1WeatherTable = AnimationUtils.loadAnimation(this, R.anim.alpha0to1);
	myAnimationAlpha1to0WeatherTable = AnimationUtils.loadAnimation(this, R.anim.alpha1to0);
	myAnimationAlpha0to1Ups = AnimationUtils.loadAnimation(this, R.anim.alpha0to1);
	myAnimationAlpha1to0NoResults = AnimationUtils.loadAnimation(this, R.anim.alpha1to0);

	loadWindowMain();
    }
    
    
    
    
    
    public void loadWindowMain() {
	setContentView(R.layout.window_main);

	myLinearLayoutSearch = (LinearLayout) findViewById(R.id.linearLayoutSearh);
	myLinearLayoutHead = (LinearLayout) findViewById(R.id.linearLayoutHeader);
	myLinearLayoutMetOffice = (LinearLayout) findViewById(R.id.linearLayoutMetOfficeLogo);
	myTextViewTypeCity = (TextView) findViewById(R.id.textViewType);
	myTextViewCopyright = (TextView) findViewById(R.id.textViewCopyright);
	
	myProgressBarLoading = (ProgressBar) ((ViewStub) findViewById(R.id.viewStubProgressBar)).inflate();

	myAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        myAutoCompleteTextView.setEnabled(false);
	myAutoCompleteTextView.setThreshold(3);
	myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		    long arg3) {
		if (mainWindow)	changeWindowFromMainToResults();
		else requestNewWeatherTable();
	    }	    
	});
	myAutoCompleteTextView.setOnKeyListener(new OnKeyListener() {
	    public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (!myAutoCompleteTextView.isPopupShowing()
			&& (event.getAction() == KeyEvent.ACTION_DOWN)
			&& (keyCode == KeyEvent.KEYCODE_ENTER))
		    if (myAutoCompleteTextView.getText().toString().length() > 2) {
			if (mainWindow)	changeWindowFromMainToResults();
			else requestNewWeatherTable();
			return true;
		    }
		    else {
			Toast.makeText(WindowMain.this,
				getString(R.string.eih), Toast.LENGTH_SHORT).show();
			return false;
		    }
		return false;
	    }
	});

	myButtonGo = (Button) findViewById(R.id.buttonGo);
	myButtonGo.setOnClickListener(new View.OnClickListener() {
	    public void onClick(View v) {
		if (myAutoCompleteTextView.getText().toString().length() > 2) {
			if (mainWindow)	changeWindowFromMainToResults();
			else requestNewWeatherTable();
		}
		else Toast.makeText(WindowMain.this,
			getString(R.string.eih), Toast.LENGTH_SHORT).show();
	    }
	});
	
	new ParseXmlCities().execute();
	
	loadWindowResults();
    }
    
    
    
    
    
    public void loadWindowResults() {
	myLinearLayoutWeatherTable = (LinearLayout) ((ViewStub) findViewById(R.id.viewStubLinearLayoutWeatherTable)).inflate();
	myLinearLayoutWeatherTable.setVisibility(View.INVISIBLE);
	myLinearLayoutUps = (LinearLayout) ((ViewStub) findViewById(R.id.viewStubLinearLayoutUps)).inflate();
	myLinearLayoutUps.setVisibility(View.INVISIBLE);
	myTextViewUps = (TextView) findViewById(R.id.textViewUps);
	myTextViewCityWeatherForecast = (TextView) findViewById(R.id.textViewCityWeatherForecast);
	myTextViewD1Day = (TextView) findViewById(R.id.textViewD1Day);
	myTextViewD1Mon = (TextView) findViewById(R.id.textViewD1Mon);
	myTextViewD1T = (TextView) findViewById(R.id.textViewD1T);
	myImageViewD1W = (ImageView) findViewById(R.id.imageViewD1W);
	myTextViewD1WS = (TextView) findViewById(R.id.textViewD1WS);
	myTextViewN1T = (TextView) findViewById(R.id.textViewN1T);
	myImageViewN1W = (ImageView) findViewById(R.id.imageViewN1W);
	myTextViewN1WS = (TextView) findViewById(R.id.textViewN1WS);
	myTextViewD2Day = (TextView) findViewById(R.id.textViewD2Day);
	myTextViewD2Mon = (TextView) findViewById(R.id.textViewD2Mon);
	myTextViewD2T = (TextView) findViewById(R.id.textViewD2T);
	myImageViewD2W = (ImageView) findViewById(R.id.imageViewD2W);
	myTextViewD2WS = (TextView) findViewById(R.id.textViewD2WS);
	myTextViewN2T = (TextView) findViewById(R.id.textViewN2T);
	myImageViewN2W = (ImageView) findViewById(R.id.imageViewN2W);
	myTextViewN2WS = (TextView) findViewById(R.id.textViewN2WS);
	myTextViewD3Day = (TextView) findViewById(R.id.textViewD3Day);
	myTextViewD3Mon = (TextView) findViewById(R.id.textViewD3Mon);
	myTextViewD3T = (TextView) findViewById(R.id.textViewD3T);
	myImageViewD3W = (ImageView) findViewById(R.id.imageViewD3W);
	myTextViewD3WS = (TextView) findViewById(R.id.textViewD3WS);
	myTextViewN3T = (TextView) findViewById(R.id.textViewN3T);
	myImageViewN3W = (ImageView) findViewById(R.id.imageViewN3W);
	myTextViewN3WS = (TextView) findViewById(R.id.textViewN3WS);
	myTextViewD4Day = (TextView) findViewById(R.id.textViewD4Day);
	myTextViewD4Mon = (TextView) findViewById(R.id.textViewD4Mon);
	myTextViewD4T = (TextView) findViewById(R.id.textViewD4T);
	myImageViewD4W = (ImageView) findViewById(R.id.imageViewD4W);
	myTextViewD4WS = (TextView) findViewById(R.id.textViewD4WS);
	myTextViewN4T = (TextView) findViewById(R.id.textViewN4T);
	myImageViewN4W = (ImageView) findViewById(R.id.imageViewN4W);
	myTextViewN4WS = (TextView) findViewById(R.id.textViewN4WS);
	myTextViewD5Day = (TextView) findViewById(R.id.textViewD5Day);
	myTextViewD5Mon = (TextView) findViewById(R.id.textViewD5Mon);
	myTextViewD5T = (TextView) findViewById(R.id.textViewD5T);
	myImageViewD5W = (ImageView) findViewById(R.id.imageViewD5W);
	myTextViewD5WS = (TextView) findViewById(R.id.textViewD5WS);
	myTextViewN5T = (TextView) findViewById(R.id.textViewN5T);
	myImageViewN5W = (ImageView) findViewById(R.id.imageViewN5W);
	myTextViewN5WS = (TextView) findViewById(R.id.textViewN5WS);
    }
    
    
    
    
    
    public void changeWindowFromMainToResults() {
	myLinearLayoutSearch.startAnimation(myAnimationTranslateUp);
	myLinearLayoutHead.startAnimation(myAnimationAlpha1to0);
	myTextViewTypeCity.startAnimation(myAnimationAlpha1to0);
	myLinearLayoutMetOffice.startAnimation(myAnimationAlpha1to0);
	myTextViewCopyright.startAnimation(myAnimationAlpha1to0);
	myLinearLayoutHead.setVisibility(View.INVISIBLE);
	myTextViewTypeCity.setVisibility(View.INVISIBLE);
	myLinearLayoutMetOffice.setVisibility(View.INVISIBLE);
	myTextViewCopyright.setVisibility(View.INVISIBLE);
    }
    
    
    
    
    
    public void changeWindowFromResultsToMain() {
	if (myLinearLayoutWeatherTable.isShown()) {
	    myLinearLayoutWeatherTable.startAnimation(myAnimationAlpha1to0WeatherTable);
	    myLinearLayoutWeatherTable.setVisibility(View.INVISIBLE);
	}
	if (myLinearLayoutUps.isShown()) {
	    myLinearLayoutUps.startAnimation(myAnimationAlpha1to0NoResults);
	    myLinearLayoutUps.setVisibility(View.INVISIBLE);
	}
	myLinearLayoutSearch.startAnimation(myAnimationTranslateDown);
    }

    
    
    
    
    private void requestNewWeatherTable() {
	myCurrentCity = myAutoCompleteTextView.getText().toString();
	cityFound = false;
	for (int i=0; i<citiesNameList.length; i++) {
	    if (myCurrentCity.equalsIgnoreCase(citiesList[i][3]))
	    {
		cityFound = true;
		myCurrentCity = citiesList[i][3]; //To match cases.
		myAutoCompleteTextView.clearFocus();
		new ParseXmlWeather().execute(Integer.parseInt(citiesList[i][0]));
		break;
	    }
	}
	if (!cityFound) {
	    if (myLinearLayoutWeatherTable.isShown()) {
		myLinearLayoutWeatherTable.startAnimation(myAnimationAlpha1to0WeatherTable);
		myLinearLayoutWeatherTable.setVisibility(View.INVISIBLE);
	    }
	    myTextViewUps.setText(getString(R.string.ups));
	    myLinearLayoutUps.startAnimation(myAnimationAlpha0to1Ups);
	    if (!myLinearLayoutUps.isShown()) myLinearLayoutUps.setVisibility(View.VISIBLE);
	}
    }
    
    
    
    
    
    public void updateWeatherTable(String weather[][]) {
	if (weather != null) {
	    myTextViewCityWeatherForecast.setText(myCurrentCity);
	    myTextViewD1Day.setText(weather[0][0]); // Month
	    myTextViewD1Mon.setText(myFormat.format(Integer.parseInt(weather[0][1]))); // Day
	    myTextViewD1T.setText(weather[0][3]+" ºC"); // Feels Like Maximum Temperature
	    myTextViewD1WS.setText(weather[0][7]+" mph"); // Wind Speed
	    myTextViewN1T.setText(weather[1][4]+" ºC");
	    myTextViewN1WS.setText(weather[1][7]+" mph");
	    myTextViewD2Day.setText(weather[2][0]);
	    myTextViewD2Mon.setText(myFormat.format(Integer.parseInt(weather[2][1])));
	    myTextViewD2T.setText(weather[2][3]+" ºC");
	    myTextViewD2WS.setText(weather[2][7]+" mph");
	    myTextViewN2T.setText(weather[3][4]+" ºC");
	    myTextViewN2WS.setText(weather[3][7]+" mph");
	    myTextViewD3Day.setText(weather[4][0]);
	    myTextViewD3Mon.setText(myFormat.format(Integer.parseInt(weather[4][1])));
	    myTextViewD3T.setText(weather[4][3]+" ºC");
	    myTextViewD3WS.setText(weather[4][7]+" mph");
	    myTextViewN3T.setText(weather[5][4]+" ºC");
	    myTextViewN3WS.setText(weather[5][7]+" mph");
	    myTextViewD4Day.setText(weather[6][0]);
	    myTextViewD4Mon.setText(myFormat.format(Integer.parseInt(weather[6][1])));
	    myTextViewD4T.setText(weather[6][3]+" ºC");
	    myTextViewD4WS.setText(weather[6][7]+" mph");
	    myTextViewN4T.setText(weather[7][4]+" ºC");
	    myTextViewN4WS.setText(weather[7][7]+" mph");
	    myTextViewD5Day.setText(weather[8][0]);
	    myTextViewD5Mon.setText(myFormat.format(Integer.parseInt(weather[8][1])));
	    myTextViewD5T.setText(weather[8][3]+" ºC");
	    myTextViewD5WS.setText(weather[8][7]+" mph");
	    myTextViewN5T.setText(weather[9][4]+" ºC");
	    myTextViewN5WS.setText(weather[9][7]+" mph");
	    updateImageViewWeather(myImageViewD1W, Integer.parseInt(weather[0][2]));
	    updateImageViewWeather(myImageViewN1W, Integer.parseInt(weather[1][2]));
	    updateImageViewWeather(myImageViewD2W, Integer.parseInt(weather[2][2]));
	    updateImageViewWeather(myImageViewN2W, Integer.parseInt(weather[3][2]));
	    updateImageViewWeather(myImageViewD3W, Integer.parseInt(weather[4][2]));
	    updateImageViewWeather(myImageViewN3W, Integer.parseInt(weather[5][2]));
	    updateImageViewWeather(myImageViewD4W, Integer.parseInt(weather[6][2]));
	    updateImageViewWeather(myImageViewN4W, Integer.parseInt(weather[7][2]));
	    updateImageViewWeather(myImageViewD5W, Integer.parseInt(weather[8][2]));
	    updateImageViewWeather(myImageViewN5W, Integer.parseInt(weather[9][2]));
	    if (myLinearLayoutUps.isShown()) {
		myLinearLayoutUps.startAnimation(myAnimationAlpha1to0NoResults);
		myLinearLayoutUps.setVisibility(View.INVISIBLE);
	    }
	    myLinearLayoutWeatherTable.startAnimation(myAnimationAlpha0to1WeatherTable);
	    if (!myLinearLayoutWeatherTable.isShown()) myLinearLayoutWeatherTable.setVisibility(View.VISIBLE);
	}
	else {
	    if (myLinearLayoutWeatherTable.isShown()) {
		myLinearLayoutWeatherTable.startAnimation(myAnimationAlpha1to0WeatherTable);
		myLinearLayoutWeatherTable.setVisibility(View.INVISIBLE);
	    }
	    myTextViewUps.setText(getString(R.string.ups_internet));
	    myLinearLayoutUps.startAnimation(myAnimationAlpha0to1Ups);
	    if (!myLinearLayoutUps.isShown()) myLinearLayoutUps.setVisibility(View.VISIBLE);
	}
    }

    
    
    
    
    public void updateImageViewWeather(ImageView image, int weatherType) {
	switch (weatherType) {
	case 0: image.setImageResource(R.drawable.w0); break;
	case 1: image.setImageResource(R.drawable.w1); break;
	case 2: image.setImageResource(R.drawable.w2); break;
	case 3: image.setImageResource(R.drawable.w3); break;
	case 4: image.setImageResource(R.drawable.w31); break;
	case 5: image.setImageResource(R.drawable.w5); break;
	case 6: image.setImageResource(R.drawable.w6); break;
	case 7: image.setImageResource(R.drawable.w7); break;
	case 8: image.setImageResource(R.drawable.w8); break;
	case 9: image.setImageResource(R.drawable.w9); break;
	case 10: image.setImageResource(R.drawable.w10); break;
	case 11: image.setImageResource(R.drawable.w11); break;
	case 12: image.setImageResource(R.drawable.w12); break;
	case 13: image.setImageResource(R.drawable.w13); break;
	case 14: image.setImageResource(R.drawable.w14); break;
	case 15: image.setImageResource(R.drawable.w15); break;
	case 16: image.setImageResource(R.drawable.w16); break;
	case 17: image.setImageResource(R.drawable.w17); break;
	case 18: image.setImageResource(R.drawable.w18); break;
	case 19: image.setImageResource(R.drawable.w19); break;
	case 20: image.setImageResource(R.drawable.w20); break;
	case 21: image.setImageResource(R.drawable.w21); break;
	case 22: image.setImageResource(R.drawable.w22); break;
	case 23: image.setImageResource(R.drawable.w23); break;
	case 24: image.setImageResource(R.drawable.w24); break;
	case 25: image.setImageResource(R.drawable.w25); break;
	case 26: image.setImageResource(R.drawable.w26); break;
	case 27: image.setImageResource(R.drawable.w27); break;
	case 28: image.setImageResource(R.drawable.w28); break;
	case 29: image.setImageResource(R.drawable.w29); break;
	case 30: image.setImageResource(R.drawable.w30); break;
	case 31: image.setImageResource(R.drawable.w31);
	}
    }

    
    
    
    
    @Override
    public void onBackPressed() {
	if (!mainWindow) changeWindowFromResultsToMain();
	else super.onBackPressed();
    }

    
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.menu, menu);
	myMenu = menu;

	// If the mobile has QWERTY keyboard we set alphabetic shortcuts to every
	//menu option.
	if (getResources().getConfiguration().keyboard ==
		Configuration.KEYBOARD_QWERTY) {
	    myMenu.findItem(R.id.menu_back).setAlphabeticShortcut('b').setTitle(getString(R.string.menu_back)+" (B)");
	    myMenu.findItem(R.id.menu_about).setAlphabeticShortcut('a').setTitle(getString(R.string.menu_about)+" (A)");
	    myMenu.findItem(R.id.menu_exit).setAlphabeticShortcut('e').setTitle(getString(R.string.menu_exit)+" (E)");
	}

	// If the mobile has numeric-12keys keyboard we set numeric shortcuts to
	//every menu option.
	if (getResources().getConfiguration().keyboard ==
		Configuration.KEYBOARD_12KEY) {
	    myMenu.findItem(R.id.menu_back).setAlphabeticShortcut('7').setTitle(getString(R.string.menu_about)+" (7)");
	    myMenu.findItem(R.id.menu_about).setAlphabeticShortcut('8').setTitle(getString(R.string.menu_about)+" (8)");
	    myMenu.findItem(R.id.menu_exit).setAlphabeticShortcut('9').setTitle(getString(R.string.menu_exit)+" (9)");
	}
	return super.onCreateOptionsMenu(menu);
    }

    
    
    
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
	myMenu = menu;
	if (mainWindow) 
	    myMenu.findItem(R.id.menu_back).setVisible(false);
	else {
	    myMenu.findItem(R.id.menu_back).setVisible(true);
	}
	return super.onCreateOptionsMenu(menu);
    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.menu_back: changeWindowFromResultsToMain(); break;
	case R.id.menu_about: startActivity(new Intent(this, WindowAbout.class)); break;
	case R.id.menu_exit: finish();
	}
	return super.onOptionsItemSelected(item);
    }
}